package com.zzmcc.jms.controller;

import com.jfinal.aop.Before;
import com.jfinal.ext.render.excel.JxlsRender;
import com.jfinal.kit.PathKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.zzmcc.base.BaseController;
import com.zzmcc.common.AuthorizeInterceptor;
import com.zzmcc.jms.model.*;
import com.zzmcc.sys.model.Userinfo;
import util.ChristStringUtil;
import util.DateUtil;
import util.ModelUtils;
import util.StringUtil;

import java.util.*;

public class CuBuildController extends BaseController {
    private static final Logger log =  Logger.getLogger(CuBuildController.class);
	private  int flag=0;
	public void index() {
		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();

		String name = getPara("name");
		String ctName = getPara("ctName");
		String cusName = getPara("cusName");
		String homeCounty = getPara("homeCounty");
		String isConfirm = getPara("isConfirm");
		String isProject = getPara("isProject");
		String buildType = getPara("buildType");
		String taskMonth = getPara("taskMonth");
		String isDesign = getPara("isDesign");
		String isReach = getPara("isReach");
		String isStart = getPara("isStart");
		String isComplete = getPara("isComplete");
		String isArchive = getPara("isArchive");
		String isSubmit = getPara("isSubmit");
		String demandMemo = getPara("demandMemo");
		String modifyDate = getPara("modifyDate");
		String isName = getPara("isName");
		String isAll = getPara("isAll");

		if(ChristStringUtil.isNotEmpty(name)){
			whee.append(" and a.name like ?");
			param.add("%" + name + "%");
		}
		if(ChristStringUtil.isNotEmpty(demandMemo)){
			whee.append(" and a.demandMemo like ?");
			param.add("%" + demandMemo + "%");
		}

		if(ChristStringUtil.isNotEmpty(taskMonth)){
			whee.append(" and a.taskMonth = ?");
			param.add(taskMonth);
		}

		if(ChristStringUtil.isNotEmpty(ctName)){
			whee.append(" and COALESCE(d.ctName,a.ctName)  like ?");
			param.add("%" + ctName + "%");
		}


		if(ChristStringUtil.isNotEmpty(cusName)){
			whee.append(" and a.cusName like ?");
			param.add("%" + cusName + "%");
		}

		if(ChristStringUtil.isNotEmpty(homeCounty)){
			whee.append(" and a.homeCounty = ?");
			param.add(homeCounty);
		}

		if(ChristStringUtil.isNotEmpty(buildType)){
			whee.append(" and c.buildType = ?");
			param.add(buildType);
		}

		if(ChristStringUtil.isNotEmpty(isConfirm)){
			whee.append(" and coalesce(a.isConfirm,'否')= ?");
			param.add(isConfirm);
		}

		if(ChristStringUtil.isNotEmpty(isProject)){
			whee.append(" and coalesce(e.isProject,'否')= ?");
			param.add(isProject);
		}
		if(ChristStringUtil.isNotEmpty(isDesign)){
			whee.append(" and coalesce(b.isDesign,'否') = ?");
			param.add(isDesign);
		}

		if(ChristStringUtil.isNotEmpty(isReach)){
			whee.append(" and coalesce(b.isReach,'否') = ?");
			param.add(isReach);
		}
		if(ChristStringUtil.isNotEmpty(isName)){
			if (isName.equals("是")){
				whee.append(" and b.reachDate<NOW()  ");
			}else{
				whee.append(" and b.reachDate is NULL");
			}
		}

		if(ChristStringUtil.isNotEmpty(isStart)){
			whee.append(" and coalesce(b.isStart,'否') = ?");
			param.add(isStart);
		}
		if(ChristStringUtil.isNotEmpty(isComplete)){
			whee.append(" and coalesce(b.isComplete,'否') = ?");
			param.add(isComplete);
		}
		if(ChristStringUtil.isNotEmpty(isSubmit)){
			whee.append(" and coalesce(b.isSubmit,'否') = ?");
			param.add(isSubmit);
		}
		if(ChristStringUtil.isNotEmpty(isArchive)){
			whee.append(" and coalesce(b.isArchive,'否') = ?");
			param.add(isArchive);
		}

		if(ChristStringUtil.isNotEmpty(modifyDate)){
			whee.append(" and TO_DAYS(b.modifyDate) >= TO_DAYS(?)");
			param.add( modifyDate );
		}

		Page<ProjectBuild> pager = ProjectBuild.dao.paginate(
				getParaToInt("pageNum", 1),
				getParaToInt("numPerPage", 20),
				"select a.id,a.ctName,a.homeCounty,a.name,u.scene,u.systemBand,u.buildType,u.longitude,u.latitude,\n" +
				"b.isReach,b.isStart,b.isComplete,\n" +
				"c.cuName,c.isReach,c.isPass,c.buildType,c.towerType,c.modifyMemo,c.memo,c.modifyDate\n",
				"from jms_demand a \n" +
				"LEFT  JOIN cu_demand u\n" +
				"on a.did=u.did\n" +
				"left join jms_project_build b on b.pid=a.pid \n" +
				"LEFT JOIN jms_cu_build c\n" +
				"on c.pid=a.pid \n" +
				"where a.cusName='联通' and (a.demandMemo like '%第八批%'or a.demandMemo like '%清单外%')\n  "+ whee.toString()+" order by c.modifyDate desc",
				param.toArray());

		setAttr("pager",pager);
		setAttr("name",name);
		setAttr("ctName",ctName);
		setAttr("cusName",cusName);
		setAttr("homeCounty",homeCounty);
		setAttr("isConfirm",isConfirm);
		setAttr("isProject",isProject);
		setAttr("buildType",buildType);
		setAttr("taskMonth",taskMonth);
		setAttr("demandMemo",demandMemo);
		setAttr("isDesign",isDesign);
		setAttr("isReach",isReach);
		setAttr("isStart",isStart);
		setAttr("isComplete", isComplete);
		setAttr("isArchive",isArchive);
		setAttr("isSubmit",isSubmit);
		setAttr("modifyDate",modifyDate);
		setAttr("isName",isName);
		setAttr("isAll",isAll);

		render("../cu_build_list.html");
	}


	@Before(AuthorizeInterceptor.class)
	public void add() {
		String id = getPara("id");
		Userinfo loginUser =getSessionAttr("loginUser");
		String homeCounty=loginUser.get("homeCounty");
		String modifyUser=loginUser.get("realname");
		int roleid=loginUser.get("roleid");

		String sql="select a.did,a.pid,a.ctName,u.id as uid,a.homeCounty,u.name,u.scene,u.systemBand,u.buildType as uBuildType,u.longitude as uLong,u.latitude as uLat,\n" +
				"b.isStart,b.isComplete,\n" +
				"c.id,c.isReach,c.isPass,c.buildType,c.towerType,c.towerHeigh,c.cuName,c.longitude,c.latitude,c.addr,c.modifyMemo,c.memo,\n" +
				"c.isLine,c.linePDate,c.isTrans,c.transPDate,c.isWireless,c.wirelessPDate,c.isEquip,c.equipPDate,c.isSite,c.sitePDate\n" +
				"from jms_demand a \n" +
				"LEFT  JOIN cu_demand u\n" +
				"on a.did=u.did\n" +
				"left join jms_project_build b on b.pid=a.pid\n" +
				"LEFT JOIN jms_cu_build c\n" +
				"on c.pid=a.pid\n" +
				" where a.id=?";
		CuBuild m= CuBuild.dao.findFirst(sql,id);


		//管理员及区域负责人才有权限修改
		if (roleid==21||(StringUtil.isNotEmpty(homeCounty)&&homeCounty.equals(m.get("homeCounty")))){
			setAttr("modifyUser",modifyUser);
			setAttr("roleid",roleid);
			setAttr("cuBuild",m);
			render("../cu_bulid_input.html");
		} else {
			render("../project_operate_tip.html");
		}
	}

	public void save() {
		try {

			Userinfo loginUser =getSessionAttr("loginUser");
			int roleid=loginUser.get("roleid");

			CuBuild m = getModel(CuBuild.class, "cuBuild");
			CuDemand cuDemand = getModel(CuDemand.class, "cuDemand");

			String isLine = m.getStr("isLine");
			if (StringUtil.isNotEmpty(isLine) && isLine.equals("是")){
				m.set("lineCDate", new Date());
			}

			String isTrans = m.getStr("isTrans");
			if (StringUtil.isNotEmpty(isTrans) && isTrans.equals("是")){
				m.set("transCDate", new Date());
			}

			String isWireless = m.getStr("isWireless");
			if (StringUtil.isNotEmpty(isWireless) && isWireless.equals("是")){
				m.set("wirelessCDate", new Date());
			}
			String isEquip = m.getStr("isEquip");
			if (StringUtil.isNotEmpty(isEquip) && isEquip.equals("是")) {
				m.set("equipCDate", new Date());
			}
			String isSite = m.getStr("isSite");
			if (StringUtil.isNotEmpty(isSite) && isSite.equals("是")){
				m.set("siteCDate", new Date());
			}

			ModelUtils.fillForUpdate(m);
			if (m.get("id")==null)
				m.save();
			else
				m.update();

			if(roleid==21){
				if (cuDemand.get("id")==null)
					cuDemand.save();
				else
					cuDemand.update();
			}

			renderDWZSuccessJson("施工进度更新成功！");
		}catch (Exception e){
			renderDWZSuccessJson("提交失败！");
		}
	}



	public void export(){

		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();

		//导出条件
		String name = getPara("name");
		String cusName = getPara("cusName");
		String homeCounty = getPara("homeCounty");
		String buildType = getPara("buildType");
		String taskMonth = getPara("taskMonth");
		String demandMemo = getPara("demandMemo");

		if(ChristStringUtil.isNotEmpty(name)){
			whee.append(" and a.name like ?");
			param.add("%" + name + "%");
		}
		if(ChristStringUtil.isNotEmpty(demandMemo)){
			whee.append(" and a.demandMemo like ?");
			param.add("%" + demandMemo + "%");
		}

		if(ChristStringUtil.isNotEmpty(cusName)){
			whee.append(" and a.cusName = ?");
			param.add(cusName);
		}

		if(ChristStringUtil.isNotEmpty(homeCounty)){
			whee.append(" and a.homeCounty = ?");
			param.add(homeCounty);
		}

		if(ChristStringUtil.isNotEmpty(buildType)){
			whee.append(" and c.buildType = ?");
			param.add(buildType);
		}

		if(ChristStringUtil.isNotEmpty(taskMonth)){
			whee.append(" and a.taskMonth = ?");
			param.add(taskMonth);
		}


		/**
		 * 清单
		 */
		String sql="select a.pid,a.ctName,a.homeCounty,a.name,u.scene,u.systemBand,u.buildType as uBuildType,u.longitude as uLong,u.latitude as uLat," +
				"b.isReach,b.reachDate,b.isStart,b.startDate,b.isComplete,b.completeDate,b.isSubmit,b.submitDate,\n" +
				"c.isReach as isAddr,c.isPass,c.buildType,CONCAT(c.towerHeigh,'米') towerHeigh,c.towerType,c.cuName,c.longitude,c.latitude,c.addr,c.modifyMemo,c.memo,\n" +
				"c.isLine,c.linePDate,c.lineCDate,\n" +
				"c.isTrans,c.transPDate,c.transCDate,\n" +
				"c.isWireless,c.wirelessPDate,c.wirelessCDate,\n" +
				"c.isEquip,c.equipPDate,c.equipCDate,\n" +
				"c.isSite,c.sitePDate,c.siteCDate\n" +
				"from jms_demand a \n" +
				"LEFT  JOIN cu_demand u\n" +
				"on a.did=u.did\n" +
				"left join jms_project_build b on b.pid=a.pid\n" +
				"LEFT JOIN jms_cu_build c\n" +
				"on a.pid=c.pid \n " +
				"where a.cusName='联通' and (a.demandMemo like '%第八批%'or a.demandMemo like '%清单外%')  "+ whee.toString()+"  order by c.modifyDate desc" ;

		List<Record> cuDetail = Db.find(sql, param.toArray());

		/**
		 * 区县汇总
		 */
		sql = "select a.homeCounty,count(*) cnt,\n" +
				"sum(case when u.buildType like '%共享%' then 1 else 0 end) oldCnt,\n" +
				"sum(case when u.buildType like '%新建%' then 1 else 0 end) newCnt,\n" +
				"sum(case when c.isReach='是' then 1 else 0 end) addrCnt,\n" +
				"sum(case when b.isReach='是' then 1 else 0 end) reachCnt,\n" +
				"sum(case when b.isComplete='是' then 1 else 0 end) completeCnt,\n" +
				"sum(case when c.isTrans='是' then 1 else 0 end) transCnt,\n" +
				"sum(case when c.isSite='是' then 1 else 0 end) siteCnt \n" +
				"from jms_demand a \n" +
				"left  JOIN cu_demand u\n" +
				"on a.did=u.did\n" +
				"left join jms_project_build b on b.pid=a.pid\n" +
				"LEFT JOIN jms_cu_build c\n" +
				"on a.pid=c.pid \n" +
				" where  a.cusName='联通' and (a.demandMemo like '%第八批%'or a.demandMemo like '%清单外%')    \n" +
				"GROUP BY a.homeCounty" ;

		List<Record> cuSum = Db.find(sql);


		Map<String, Object> beans = new HashMap<String, Object>();
		beans.put("cuDetail", cuDetail);
		beans.put("cuSum", cuSum);

		beans.put("homeCounty",homeCounty);

		String templateFileName = PathKit.getRootClassPath() + "/tpl_cu_build.xls";
		String filename = "cu_build.xls";
		render(JxlsRender.me(templateFileName).filename(filename).beans(beans));
	}


	@Before(AuthorizeInterceptor.class)
	public void delete() {
		String[] ids = getParaValues("ids");
		for(String id:ids){
			Project m =Project.dao.findById(id);
			Db.update("delete a from jms_project_build a where pid =?",m.get("pid"));
		}
		renderDWZSuccessJson("清除成功!");
	}


}